﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Library.DomainObject.SQLServer;
using System.Data;
using Dapper;

namespace Web.Library.DataAccess.SQLServer
{
    public partial class SysMenuRepo
    {
        public List<SysMenu> GetListByParentID(int parentID)
        {
            List<SysMenu> tt = new List<SysMenu>();
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    string strQuery = "SELECT MenuID, ParentID, Title, Url, Sort, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM dbo.SysMenu with(nolock) WHERE ParentID=@ParentID";
                   tt = conn.Query<SysMenu>(strQuery, new { ParentID = parentID }).ToList<SysMenu>();
                   
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }
            return tt;
        }

        public SysGroupAuthorization SysGroupAuthorizationByGroupIDAndMenuID(int GroupID, int MenuID)
        {
            SysGroupAuthorization t = null;
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                string strQuery = "SELECT AuthID, GroupID, MenuID, AllowDisplay, AllowUpdate, AllowCreate, AllowDelete, CreatedDate, CreteadBy, UpdatedDate, UpdatedBy FROM dbo.SysGroupAuthorization with(nolock) WHERE GroupID = @GroupID AND MenuID = @MenuID";
                try
                {
                    conn.Open();
                    t= conn.Query<SysGroupAuthorization>(strQuery, new {GroupID = GroupID,MenuID = MenuID }).SingleOrDefault();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }

            }
            return t;
        }

        public SysMenu GetByUrl(string Url)
        {
            SysMenu t = null;
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                string strQuery = "SELECT TOP 1 MenuID, ParentID, Title, Url, Sort, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM dbo.SysMenu with(nolock) WHERE Url = @Url";
                try
                {
                    conn.Open();
                    t = conn.Query<SysMenu>(strQuery, new { Url = Url }).SingleOrDefault();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }

            }
            return t;
        }
    }
}
